package com.oauth.util;

import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuer;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by dell on 2016/9/29.
 */
public class AuthUtil {

    public static String getMd5Code() throws OAuthSystemException {
        OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
        return oauthIssuerImpl.authorizationCode();
    }

    public static String getMd5Token() throws OAuthSystemException {
        OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
        return oauthIssuerImpl.accessToken();
    }

    public static String getMd5RefreshToken() throws OAuthSystemException {
        OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
        return oauthIssuerImpl.refreshToken();
    }

    public static OAuthResponse convert(BasicOAuthToken basicOAuthToken) throws OAuthSystemException {
        OAuthResponse response =OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK)
                .setAccessToken(basicOAuthToken.getAccessToken())
                .setExpiresIn(String.valueOf(basicOAuthToken.getExpiresIn()))
                .setTokenType(basicOAuthToken.getTokenType())
                .setRefreshToken(basicOAuthToken.getRefreshToken())
                .buildJSONMessage();
        return response;
    }
}
